Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Calling a function recursively in the context of "export default"?

This may be obvious to others but I don't know the syntax for calling a function recursively in the following code:

//request.js
    export default {

      send() {
       ... do stuff ...
       this.send(); // Can't call "send" of undefined
      }
    }

//main.js
import request from './request'

export default class Main extends Component {
   init() {
      request.send();
  }
}

In the above example, I'm calling the send function from main.js. All this is working fine but I have a condition in the send function that will recall the same function. The problem is that I have no idea how to call the function from within the function. Anybody?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you want to call the default export recursively in Node.js, using Babel.js to transpile, you can use the exports object:

// request.js

export default {
  send() {
    console.log('Yo');
    setTimeout(() => {
      exports.default.send();
    }, 1000);
  },
};

Importing:

// main.js

import request from './request';

request.send(); // Writes "Yo" to the console once per second

This is because Babel.js simply transforms to the CommonJS syntax. This:

export default 42;

Becomes this:

Object.defineProperty(exports, "__esModule", {
  value: true
});

exports.default = 42;

No idea how this may change in a future implementation of ES6 Modules in Node.js.

over 4 years ago · Santiago Trujillo Report

0

I don't see any reason why this.send() wouldn't work, given you are exporting an object with a method.

But if it doesn't work, you can always use a named function expression:

export default {
  send: function send() {
    … send() …
  }
};

or a named function declaration:

function send() {
  … send() …
}
export default { send };

or just name your exported object:

export default const request = {
  send() {
    … request.send() …
  }
};

But I would recommend to not default-export an object at all, and use named exports instead so that you can import them separately or on a namespace object:

// request.js
export default function send() {
  … send() …
}

// main.js
import * as request from './request'
…
request.send()
over 4 years ago · Santiago Trujillo Report

0

well just as a quick glimpse, I would say that you should

 console.log(this)

to see the value of it in the first and second request. I didn't build your code to check it, but I assume it is an issue with your context.

Still not sure what you try to achieve, but you can always use

 someFunction.bind(this)

again it is quite vague but maybe it will give you an idea.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!